<?php
/**
 * 购物车模型
 * @author tangbl
 *
 */
class MemberCartModel extends Model {
	
	/**
	 * 获取确认订单的商品列表
	 */
	public function getConfirmList($select){
	
		$list = $this->getUserCartList();

		$total = 0;$weight = 0;$amount=0;
		foreach($list as $k=>$v){
			if(in_array($v['id'],$select)){
				$total += intval($v['num']) * doubleval($v['sprice']);
				$weight+= intval($v['num']) * intval($v['weight']);
				$amount+= intval($v['num']);
			}else{
				unset($list[$k]);
			}
		}
		$array['amount']= $amount;
		$array['weight']= $weight;
		$array['total'] = $total;
		$array['list']  = $list;
	
		return $array;
	}
	
	/**
	 * 获取用户购物信息
	 */
	public function getUserCart($flag=true,$id=0){
		
		$list  = $this->getUserCartList();
		
		$array = $this->getUserCartTotal($list,$id);
		
		if($flag){
			$array['list'] = $list;
		}
		
		return $array;
	}
	
	/**
	 * 获取用户购物车列表
	 */
	public function getUserCartList(){
		
		$user  = getMember();
		$model = D("CartProductView");
		$map['userid'] = $user['id'];
		
		$list = $model->where($map)->order('id desc')->select();
		return $list;
	}
	
	/**
	 * 获取购物车的统计
	 */
	public function getUserCartTotal($list,$id=0){
	
		$total  = 0;
		$amount = 0;
		$width  = 0;
		foreach($list as $v){
			$price   = doubleval($v['sprice']) * intval($v['num']);
			$total  += $price;
			$amount += intval($v['num']);
			$width++;
			
			if($v['id'] == $id){
				$nprice = $price;
			}
		}
		
		$array['price']  = number_format($nprice,2);
		$array['total']  = number_format($total,2);
		$array['amount'] = $amount;
		$array['width']  = $width;
		return $array;
	}
	
	/**
	 * 添加购物车
	 * @param $id  int 商品编号
	 * @param $num int 商品数量
	 */
	public function addcart($id,$num){
		
		$user = getMember();
		$map['userid'] = $user['id'];
		$map['pid']    = $id;
		$row = $this->where($map)->find();
		
		if($row['id']){
			
			$map['id'] = $row['id'];
			$id        = $this->where($map)->setInc('num',$num);
		}else{
			
			$total = $this->where("userid=".$user['id'])->count("id");
			if(20 <= $total){
				return array("flag"=>false,"info"=>L("cat_ele_addfail2"));
			}
			
			$module = M("Product");
			$one    = $module->find($id);
			
			$array['pid']     = $id;
			$array['addtime'] = time();
			$array['userid']  = $user['id'];
			$array['type']    = $user['type'];
			$array['num']     = $num;
			$array['sprice']  = $one['sprice'];
			$array['title']   = $one['title'];
			
			$id = $this->add($array);
		}
		if($id){
			return array("flag"=>true,"info"=>L('cat_ele_addsuccess'));
		}else{
			return array("flag"=>false,"info"=>L('cat_ele_addfail'));
		}
		
	}
	
	/**
	 * 改变购物车商品数量
	 * @param $id  int 购物车主键编号
	 * @param $num int 购物车主键编号
	 */
	public function change($id,$num=1){
	
		$user = getMember();
		$map['userid'] = $user['id'];
		$map['id']     = $id;
		$map['num']    = $num;
		
		return $this->save($map);
	}
	
	/**
	 * 删除购物车商品
	 * @param $id int 购物车主键编号
	 */
	public function delcart($id){
		
		$user = getMember();
		$map['userid'] = $user['id'];
		$map['id']     = $id;
		
		return $this->where($map)->delete();
	}
	
	/**
	 * 清空购物车
	 */
	public function remove(){
		
		$user = getMember();
		$map['userid'] = $user['id'];
		
		return $this->where($map)->delete();
	}
	
	
	
	/**
	 * 获取当前会员团购秒杀详情
	 */
	public function getLottery($userid,$pid,$ordertype){
		$map['userid']    = $userid;
		$map['tid']       = $pid;
		$map['ordertype'] = $ordertype;
	
		$data = M("Lottery")->where($map)->find();
		return $data;
	}
	
	/**
	 * 获取团购条件-总购买数量
	 */
	public function getTuanNum($id){
		$cart       = D("MemberCart");
		$map['tid'] = $id;
		$tuannum    = $cart->where($map)->count("num");
		return $tuannum;
	}
	
	/**
	 * 获取团购条件-个人数量
	 * @param  $id      int  团购id
	 * @param  $userid  int  用户id
	 * @return $personnum 数组
	 */
	public function getUserTuanNum($id,$userid,$ordertype){
		$cart = D("MemberCart");
	
		$map['tid']       = $id;
		$map['userid']    = $userid;
		$map['ordertype'] = $ordertype;
	
		$personnum = $cart->where($map)->find();       //个人购买数量查询
		return $personnum;
	}
	
	
}
?>